Skip to content

refactor: convert thiserror to snafu and remove anyhow in public apis #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2025

Conversation

ramfox
Copy link

@ramfox ramfox commented Jun 17, 2025

Attempting to convert errors to snafu errors. lmk if you have opinions about this that are different from what i'm currently doing!

@ramfox ramfox changed the title refactor: convert thiserror to snafu and remove anyhow in public apis WIP refactor: convert thiserror to snafu and remove anyhow in public apis Jun 17, 2025
@ramfox ramfox marked this pull request as draft June 17, 2025 21:55
@ramfox ramfox force-pushed the ramfox/snafu-errors branch from e54830f to 5ba20cc Compare June 21, 2025 00:31
@ramfox ramfox force-pushed the ramfox/snafu-errors branch from 5ba20cc to 4fce71e Compare June 24, 2025 20:21
@ramfox
Copy link
Author

ramfox commented Jun 24, 2025

@rklaehn okay, so I've done all the errors...except GetError. I tried my best for the rest of the errors to maintain everything as closely as possible to the structure that already existed.

The typical conversion should go like something like this:

.map_err(|e| GetError::LocalFailure(e.into()))?

becomes:

.map_err(anyhow::Error) // because all the sources are `anyhow:Error` in the `GetError` enum
.context(LocalFailureSnafu)?

But in src/api/remote.rs, for example, we can't seem to get access to LocalFailureSnafu, no matter what pub visibility level I make the get::error module. I'm at a loss. There may be something obvious I'm missing but I can't figure it out.

Asking for help!

@ramfox ramfox changed the title WIP refactor: convert thiserror to snafu and remove anyhow in public apis refactor: convert thiserror to snafu and remove anyhow in public apis Jun 24, 2025
@rklaehn
Copy link
Collaborator

rklaehn commented Jun 25, 2025

I think the visibility thing is just because the error module is not publicly exported. I just use the module as a way to structure the code, but then export just the error one level up.

I now have 2 branches where I got snafu everywhere, but I am not happy with any of them.

In this branch: https://github.com/n0-computer/blobs2/tree/snafu-errors I just make GetError a big list of things that can go wrong. But that was not what it was meant to be. A user now has to look into each of the cases and check each of the sub cases to see if e.g. the error is recoverable (Io) or indicates a hopeless node (NonCompliantNode). I guess I could make an enum ErrorCategory and have a fn where I do this for the users.

In this branch: https://github.com/n0-computer/blobs2/tree/snafu-errors-2nd-attempt I keep the structure as it is and make a XXXCases error enum that is the sum of all sub-cases for that particular case. It does the job, but I find it super verbose.

@rklaehn
Copy link
Collaborator

rklaehn commented Jun 25, 2025

I think the second one is the better option: #2

@ramfox
Copy link
Author

ramfox commented Jun 25, 2025

@rklaehn I agree, #2 looks good

@ramfox
Copy link
Author

ramfox commented Jun 25, 2025

@rklaehn i'm in favor of closing this PR for #2

@rklaehn rklaehn merged commit 4fce71e into n0-computer:main Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants